package com.camps.sdk.encrypt;

import com.camps.sdk.common.ExceptionCode;
import com.camps.sdk.entity.WorkMode;
import com.camps.sdk.exception.BaseException;

/**
 * @Author ldaoliang
 * @Date create in 10:11 2021/9/1
 * @Description
 */
public class WorkModeCommand implements BuildCommand {
    @Override
    public String splicingParameters(String imei,Object params) {
        if(!(params instanceof WorkMode)){
            throw new BaseException(ExceptionCode.PARAMETER_ERROR);
        }
        WorkMode workMode = (WorkMode)params;
        String modelSeq = workMode.getMode();
        Integer interval = workMode.getInterval();
        String commandNumber = workMode.getCommandNumber();
        //判断工作模式与时间是否匹配，如果不匹配，则会采用默认值
        if("m1".equals(modelSeq)){
            if(interval == null || interval > 300){
                interval = 30;
            }
        }else if("m2".equals(modelSeq)){
            if(interval == null || (interval > 65535 && interval < 300)){
                interval = 600;
            }
        }else{
            interval = 65535;
        }
        // 构建指令
        StringBuilder sb = new StringBuilder("*WT,");
        sb = sb.append(imei).append(",MODE,").append(commandNumber).append(modelSeq).append(",").append(interval).append("#");
        return sb.toString();
    }
}
